/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui;

import com.vaadin.addon.touchkit.ui.NavigationBar;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

public class LoginView extends CssLayout implements ClickListener {
    
    private static final long serialVersionUID = 1L;
    TextField textLogin = null;
    PasswordField textPassword = null;
    NavigationBar navigationBar = null;
    Button btnLogin = null;
    private VerticalLayout layout = null;
    
    public LoginView() {
        setSizeFull();
        setStyleName("login");
    }
    
    public void initUI() {
        navigationBar = new NavigationBar();
        btnLogin = new Button("Login", this);
        navigationBar.setWidth("100%");
        navigationBar.setCaption("TouchProcess");
        navigationBar.setRightComponent(btnLogin);
        addComponent(navigationBar);
        
        layout = new VerticalLayout();
        textLogin = new TextField("Username:");
        textLogin.setRequired(true);
        textPassword = new PasswordField("Password:");
        textPassword.setRequired(true);
        
        
        Label up = new Label();
        up.setHeight("100%");
        
        Label bottom = new Label();
        bottom.setHeight("100%");
        
        layout.addComponent(up);
        layout.addComponent(textLogin);
        layout.setComponentAlignment(textLogin, Alignment.MIDDLE_CENTER);
        layout.addComponent(textPassword);
        layout.setComponentAlignment(textPassword, Alignment.MIDDLE_CENTER);
        layout.addComponent(textPassword);
        layout.setComponentAlignment(textPassword, Alignment.MIDDLE_CENTER);
        layout.addComponent(bottom);
        layout.setExpandRatio(bottom, 1f);
        addComponent(layout);

        // test
//        VerticalComponentGroup vcg = new VerticalComponentGroup();
//        
//        
//        for (int i = 0; i < 5; i++) {
//            HorizontalLayout lay = new HorizontalLayout();
//            lay.setWidth("100%");
//            CheckBox cb = new CheckBox();
//            cb.setWidth("30px");
//            Label label = new Label("HELLO " + i);
//            lay.addComponent(cb);
//            lay.setComponentAlignment(cb, Alignment.MIDDLE_RIGHT);
//            lay.addComponent(label);
//            lay.setComponentAlignment(label, Alignment.MIDDLE_LEFT);
//            lay.setExpandRatio(label, 1f);
//            vcg.addComponent(lay);
//            lay.addListener(new LayoutClickListener() {
//
//                public void layoutClick(LayoutClickEvent event) {
//                    HorizontalLayout clickedLay = (HorizontalLayout) event.getComponent();
//                    CheckBox clickedCb = (CheckBox) clickedLay.getComponent(0);
//                    Boolean value = (Boolean) clickedCb.getValue();
//                    clickedCb.setValue(!value);
//                }
//            });
//        }
//        vcg.setWidth("400px");
//        addComponent(vcg);
        
    }
    
    @Override
    public void attach() {
        super.attach();
    }
    
    public void buttonClick(ClickEvent event) {
        if (event.getButton().equals(btnLogin)) {
            try {
                TouchProcessApplication.getCurrent().authenticate(textLogin.getValue().toString(), textPassword.getValue().toString());
            } catch (Exception ex) {
                TouchProcessApplication.getCurrent().getMainWindow().showNotification(ex.getMessage(), Notification.TYPE_ERROR_MESSAGE);
            }
        }
    }
}
